package com.pmcc.core.config.security;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.pmcc.core.entity.security.Role;
import com.pmcc.core.entity.security.Employee;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    /**
     * 人员实体对象转成JWTUser
     * @param employee
     * @param role
     * @return
     */
    public static JwtUser create(Employee employee, Role role) {


        return new JwtUser(
                employee.getId(),
                employee.getUserName(),
                employee.getEmail(),
                employee.getSysRoleId(),
                employee.getPassword(),
                stringArrayToList(role),//给jwtuser赋值菜单的key
                true,
                null,
                employee.getUserCname()
        );
    }

    private static List stringArrayToList(Role role) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        if(role !=null){
            //获取菜单的keys
//            String keys=role.getMenuKey();
//            String[] key=keys.split(",");
            //循环放置key
//            for(String s:key){
//                grantedAuthorities.add(new SimpleGrantedAuthority(s));
//            }
            grantedAuthorities.add(new SimpleGrantedAuthority(role.getCode()));
        }
        return grantedAuthorities;
    }
}
